/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package struts;

import Service.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author jo
 */
public class RegisterAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String action=request.getParameter("action");
        if("validateEmail".equals(action)){
System.out.println("action = validate email");
            try{
                String email = request.getParameter("email");
System.out.println("email = "+ email);

                response.setContentType("text/xml");
                response.setHeader("Cache-Control", "no-store"); //HTTP1.1
                response.setHeader("Pragma", "no-cache"); //HTTP1.0
                response.setDateHeader("Expires", 0); //prevents catching at proxy server
                //check the database
                MemberService ms= new MemberServiceImpl();
                
                boolean emailExsit = ms.checkEmail(email);
System.out.println(emailExsit);
                if(emailExsit == true) {
                    response.getWriter().write("<msg>invalid</msg>");
                }
                else {
                    response.getWriter().write("<msg>valid</msg>");
                }
            }catch(Exception ex){

            }
        }else if("register".equals(action)){
System.out.println("register successfully");
            String email;
            String password;
            String firstname;
            String lastname;
            String gender;

            RegisterActionForm raf = (RegisterActionForm)form;
            email = raf.getEmail();
            password = raf.getPassword();
            firstname = raf.getFirstname();
            lastname = raf.getLastname();
            //gender = raf.getGender();
            gender = request.getParameter("gender");
            //gender = "female";
System.out.println("register action triggered");

            MemberServiceImpl registerService = new MemberServiceImpl();
System.out.println("register email" +email);
System.out.println("register password" +password);
System.out.println("register gender" +gender);
            boolean registerResult = registerService.register(email, password, firstname, lastname, gender);
            System.out.println("register result" +registerResult);
            if(true == registerResult){
                //response.setContentType("text/xml");

                return mapping.findForward("register_success");
            }else{
                return mapping.findForward("register_failed");
            }
        }
        return super.execute(mapping, form, request, response);
    }

}
